package ru.spb.osll.myfit.fixtures;

import java.util.LinkedList;
import java.util.Queue;

import org.apache.log4j.Logger;

import ru.spb.osll.myfit.ssh.SSHClientManager;
import ru.spb.osll.myfit.ssh.SSHFixturesContainer;
import fit.Parse;
import fitlibrary.DoFixture;

public class SshFixture extends DoFixture {
  private Logger log = Logger.getLogger(getClass());
  private SSHClientManager mySSHClientManager;
  Queue<String> response = new LinkedList<String>();

  @Override
  public void doTable(Parse arg0) {
    super.doTable(arg0);
  }

  public SshFixture() throws Exception{
    super();
    mySSHClientManager = SSHFixturesContainer.getSSHClientManager();
    if(mySSHClientManager == null){
      log.error("Connection is not established.");
      throw new Exception("Connection is not established.");
    }
  }
  
  public boolean sendCommand(String command) throws Exception {
    response.addAll(mySSHClientManager.executeCommand(command));
    log.info("Send command: " + command);
    return true;
  }

  public boolean expect(String expectLine) throws Exception {
    while (!response.isEmpty()) {
      String line = response.poll();
      if (line.matches(".*" + expectLine +".*")) {
        return true;
      }
    }
    
    return false;
  }

  public static String[] getTestTableTemplates() {
    return new String[] {"|Ssh|\n|send command||\n|expect||"};
  }
}
